// TypeScript file
class GameFightOneView extends GameFightView
{
    private showXin: number = 0;
    constructor()
    {
        super();
        GameData.bgSpeed = 3;
        this.totalEnemyNum = 40;
        this.boshu = 1;
        this.oneToTwo = 15;
        this.curScene = 1;
        GameData.enemySpeed = 6;
        this.timeBoo = 0;
        this.showEnemyTime = 35;
        this.showResizeBtn = 35;
        this.totalEnemy = 0;
        this.freeTime = 6;
        this.showXin = Math.floor(Math.random() * (this.totalEnemyNum - this.oneToTwo) + this.oneToTwo);
        egret.Ticker.getInstance().register(this.showEnemyFun, this);
    }

    private showEnemyFun(): void
    {
        if (!this.isShowTwoEnemy) return;

        ++this.showEnemyFunNum;

        if (this.showEnemyFunNum == 10)
        {
            this.showEnemyFunNum = 0;
            this.showEnemyTime = Math.floor(Math.random() * 20 + 20);
        }
    }

    public showTime(): void
    {
        this.isShowTwoEnemy = true;
        GameData.enemySpeed = 7.5;
    }

    public hitOver(enemy: Enemy, arr: Array<any> = [], idx: number = 0): void
    {
        switch (enemy.type)
        {
            case EnemyType.lang:
            {
                enemy.gotoDie();
                enemy.stopMove = true;
                ++GameData.langNum;
            }
            break;
            case EnemyType.daoju1:
            {
                enemy.alphaToZero();
                enemy.stopMove = true;
                GameData.blood += 3;

                if (GameData.blood > GameData.maxBlood) GameData.blood = GameData.maxBlood;
                this.bloodBar.scaleBlodX();
            }
            break;
            default:
            break;
        }
    }

    public createEnemy(): void
    {
        GameData.stopCreateEnemy = 1;
        switch (this.boshu)
        {
            case 1:
            {
                this.initEnemy(1);
            }
            break;
            case 2:
            {
                this.initEnemy(1);
            }
        }
    }

    public initEnemy(type: number): void
    {
        let enemy: Enemy = new Enemy(type);
        ++this.totalEnemy;

        ++GameData.redGirlDistance;
        this.enemySp.addChild(enemy);
        let rand: number = Math.random();
        let b: number = Math.floor(rand * 4 + 1);
        enemy.row = b;
       
        enemy.x = this.btnArr[b-1].x + this.widthPoint;
        enemy.name = "enemy1_1" + this.totalEnemy;
        this.pushEnemy(enemy.row, enemy);

        if (!this.isShowTwoEnemy)
        {
            return;
        }

        if (this.totalEnemy == this.showXin)
        {
            let enemy2: Enemy = new Enemy(6);
            this.enemySp.addChild(enemy2);
            enemy2.name = "enemy2_d" + this.totalEnemy;
            this.isShowDaoJu(enemy2, enemy);
            return;
        }

        let n: number = Math.floor(Math.random() * 4 + 1);
        if (n == 1)
        {
            let enemy3: Enemy = new Enemy(1);
            this.enemySp.addChild(enemy3);
            enemy3.name = "enemy2_1" + this.totalEnemy;
            this.isShowDaoJu(enemy3, enemy);
        }
    }

    public isShowDaoJu(enemy2: Enemy = null, enemy1: Enemy = null): void
    {
        let nn: number = enemy1.row;
        let num1: number = 0;
        let num2: number = enemy2.row;
        switch (nn)
        {
            case 1:
            {
                num1 = 3;
                num2 = 2;
            }
            break;
            case 2:
            {
                num1 = 2;
                num2 = 3;  
            }
            break;
            case 3:
            {
                num1 = 2;
                num2 = 1;    
            }
            break;
            case 4:
            {
                num1 = 3;
                num2 = 1;
            }
            break;
        }
        let b: number = Math.floor(Math.random() * num1 + num2);
        enemy2.row = b;
        enemy2.x = this.btnArr[b-1].x + this.widthPoint;
        this.pushEnemy(enemy2.row, enemy2);
    }

    public pushEnemy(row: number = 0, enemy: Enemy = null): void
    {
        switch (row)
        {
            case 1:
            {
                this.oneEnemyArr.push(enemy);
            }
            break;
            case 2:
            {
                this.twoEnemyArr.push(enemy);
            }
            break;
            case 3:
            {
                this.threeEnemyArr.push(enemy);
            }
            break;
            case 4:
            {
                this.fourEnemyArr.push(enemy);
            }
            break;
        }
    }

    public over(): void
    {
        egret.Ticker.getInstance().unregister(this.showEnemyFun, this);
        this.isStart = false;
        GameData.curScene = 2;
        egret.Tween.removeAllTweens();
        this.dispose();
        GameData.isPause = true;
        GameSceneView.gameScene.play();
    }

    public initBoShu(): void
    {
        this.timeBoo = 0;
        if (this.totalEnemy > this.totalEnemyNum)
        {
            this.stopGame = true;
            let isWin: boolean = true;
            for (let i: number =0; i < 4; ++i)
            {
                if (GameFightView.allArr[i].length != 0)
                {
                    isWin = false;
                    break;
                }
            }

            if (isWin)
            {
                this.gameWin();
            }
        }
        else if (this.totalEnemy == this.oneToTwo)
        {
            GameData.stopCreateEnemy = this.freeTime+ 10;
            ++GameData.count;

            ++GameData.redGirlDistance;
            if (GameData.count > this.freeTime)
            {
                this.boshu = 2;
                GameData.count = 0;
                GameData.stopCreateEnemy = 0;
                this.showTime();
            }
        }
    }
    
}